package com.rmy.queue.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.rmy.common.core.domain.AjaxResult;
import com.rmy.common.core.lang.IdGenerator;
import com.rmy.common.utils.DateUtils;
import com.rmy.common.utils.SecurityUtils;
import com.rmy.common.utils.StringUtils;
import com.rmy.queue.common.ErrorTipsConst;
import com.rmy.queue.domain.*;
import com.rmy.queue.mapper.QmsWindowBerrMapper;
import com.rmy.queue.service.IQmsBusinessConfigService;
import com.rmy.queue.service.IQmsWindowBusinessService;
import com.rmy.system.domain.SysUserRole;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rmy.queue.mapper.QmsWindowInfoMapper;
import com.rmy.queue.service.IQmsWindowInfoService;
import org.springframework.util.Assert;

/**
 * 鹤位信息Service业务层处理
 *
 * @author rmy
 * @date 2022-09-29
 */
@Service
public class QmsWindowInfoServiceImpl implements IQmsWindowInfoService
{
    @Autowired
    private QmsWindowInfoMapper qmsWindowInfoMapper;

    @Autowired
    private QmsWindowBerrMapper qmsWindowBerrMapper;

    @Autowired
    private IQmsBusinessConfigService qmsBusinessConfigService;

    @Autowired
    private IQmsWindowBusinessService qmsWindowBusinessService;

    @Autowired
    private IdGenerator idGenerator;

    /**
     * 查询鹤位信息
     *
     * @param id 鹤位信息主键
     * @return 鹤位信息
     */
    @Override
    public QmsWindowInfo selectQmsWindowInfoById(Long id)
    {
        return qmsWindowInfoMapper.selectQmsWindowInfoById(id);
    }

    @Override
    public QmsWindowInfo getDetailInfo(Long id) {
        QmsWindowInfo qmsWindowInfo = this.selectQmsWindowInfoById(id);
        if (null == qmsWindowInfo){
            qmsWindowInfo = new QmsWindowInfo();
            qmsWindowInfo.setId((long) 0);
        }
        QmsWindowBusiness qmsWidnowBusiness = new QmsWindowBusiness();
        qmsWidnowBusiness.setWindowId(qmsWindowInfo.getId());
        List<QmsBusinessConfig> qmsBusinessConfigList = qmsBusinessConfigService.selectQmsBusinessConfigList(new QmsBusinessConfig());
        List<QmsWindowBusiness> qmsWidnowBusinessList = new ArrayList<>();
        for (QmsBusinessConfig qmsBusinessConfig : qmsBusinessConfigList) {
            qmsWidnowBusiness.setBusinessId(qmsBusinessConfig.getId());
            List<QmsWindowBusiness> tmpList = qmsWindowBusinessService.selectQmsWindowBusinessList(qmsWidnowBusiness);
            QmsWindowBusiness tmpQwb = new QmsWindowBusiness();
            if (null != tmpList && tmpList.size() > 0){
                tmpQwb = tmpList.get(0);
            }
            tmpQwb.setBusinessName(qmsBusinessConfig.getBusinessName());
            tmpQwb.setBusinessId(qmsBusinessConfig.getId());
            qmsWidnowBusinessList.add(tmpQwb);
        }
        qmsWindowInfo.setQmsWidnowBusinessList(qmsWidnowBusinessList);
        return qmsWindowInfo;
    }

    @Override
    public QmsWindowInfo selectQmsWindowInfoByUUID(String uuid) {
        return qmsWindowInfoMapper.selectQmsWindowInfoByUUID(uuid);
    }

    /**
     * 查询鹤位信息列表
     *
     * @param qmsWindowInfo 鹤位信息
     * @return 鹤位信息
     */
    @Override
    public List<QmsWindowInfo> selectQmsWindowInfoList(QmsWindowInfo qmsWindowInfo)
    {
        return qmsWindowInfoMapper.selectQmsWindowInfoList(qmsWindowInfo);
    }

    /**
     * 新增鹤位信息
     *
     * @param qmsWindowInfo 鹤位信息
     * @return 结果
     */
    @Override
    public int insertQmsWindowInfo(QmsWindowInfo qmsWindowInfo)
    {
        qmsWindowInfo.setCreateTime(DateUtils.getNowDate());
        return qmsWindowInfoMapper.insertQmsWindowInfo(qmsWindowInfo);
    }

    @Override
    public int addQmsWindowInfo(QmsWindowInfo qmsWindowInfo) throws Exception {
        int nRet = 1;
        // 先校验鹤位信息是否存在
        if (this.checkWindowExist(qmsWindowInfo) > 0) {
            throw new Exception("鹤位号已经存在！");
        }
        qmsWindowInfo.setUuid(idGenerator.nextIdStr());
        qmsWindowInfo.setCreateBy(SecurityUtils.getUsername());
        // 默认未登录
        qmsWindowInfo.setRunningStatus("1");
        if (this.insertQmsWindowInfo(qmsWindowInfo) <= 0) {
            throw new Exception("插入鹤位信息异常！");
        }

        for (QmsWindowBusiness qmsWidnowBusiness : qmsWindowInfo.getQmsWidnowBusinessList()) {
            if (StringUtils.isEmpty(qmsWidnowBusiness.getPrioritySet())) continue;
            QmsWindowBusiness tmpSearch = new QmsWindowBusiness();
            tmpSearch.setBusinessId(qmsWidnowBusiness.getBusinessId());
            tmpSearch.setWindowId(qmsWindowInfo.getId());
            List<QmsWindowBusiness> resList = qmsWindowBusinessService.selectQmsWindowBusinessList(tmpSearch);
            qmsWidnowBusiness.setWindowId(qmsWindowInfo.getId());
            if (resList == null || resList.size() == 0) {
                qmsWindowBusinessService.insertQmsWindowBusiness(qmsWidnowBusiness);
            } else {
                qmsWindowBusinessService.updateQmsWindowBusiness(qmsWidnowBusiness);
            }
        }
        return nRet;
    }

    /**
     * 修改鹤位信息
     *
     * @param qmsWindowInfo 鹤位信息
     * @return 结果
     */
    @Override
    public int updateQmsWindowInfo(QmsWindowInfo qmsWindowInfo)
    {
        return qmsWindowInfoMapper.updateQmsWindowInfo(qmsWindowInfo);
    }

    @Override
    public int editQmsWindowInfo(QmsWindowInfo qmsWindowInfo) {
        List<QmsWindowBusiness> tmpList = qmsWindowInfo.getQmsWidnowBusinessList();
        if (tmpList != null && tmpList.size() > 0) {
            for (QmsWindowBusiness qmsWindowBusiness : tmpList) {
                QmsWindowBusiness tmpSearch = new QmsWindowBusiness();
                tmpSearch.setBusinessId(qmsWindowBusiness.getBusinessId());
                tmpSearch.setWindowId(qmsWindowInfo.getId());
                List<QmsWindowBusiness> resList = qmsWindowBusinessService.selectQmsWindowBusinessList(tmpSearch);
                qmsWindowBusiness.setWindowId(qmsWindowInfo.getId());
                if (resList == null || resList.size() == 0) {
                    if (StringUtils.isNotEmpty(qmsWindowBusiness.getPrioritySet())) {
                        qmsWindowBusinessService.insertQmsWindowBusiness(qmsWindowBusiness);
                    }
                } else {
                    if (StringUtils.isEmpty(qmsWindowBusiness.getPrioritySet())) {
                        qmsWindowBusinessService.deleteByWindowIdAndBusinessId(qmsWindowBusiness);
                    } else {
                        qmsWindowBusinessService.updateQmsWindowBusiness(qmsWindowBusiness);
                    }
                }
            }
        }
        return updateQmsWindowInfo(qmsWindowInfo);
    }

    @Override
    public int updateQmsWindowInfoByUuid(QmsWindowInfo qmsWindowInfo) {
        return qmsWindowInfoMapper.updateQmsWindowInfoByUuid(qmsWindowInfo);
    }

    /**
     * 批量删除鹤位信息
     *
     * @param ids 需要删除的鹤位信息主键
     * @return 结果
     */
    @Override
    public int deleteQmsWindowInfoByIds(Long[] ids)
    {
        return qmsWindowInfoMapper.deleteQmsWindowInfoByIds(ids);
    }

    /**
     * 删除鹤位信息信息
     *
     * @param id 鹤位信息主键
     * @return 结果
     */
    @Override
    public int deleteQmsWindowInfoById(Long id)
    {
        return qmsWindowInfoMapper.deleteQmsWindowInfoById(id);
    }

    @Override
    public Integer checkWindowExist(QmsWindowInfo qmsWindowInfo) {
        return qmsWindowInfoMapper.checkWindowExist(qmsWindowInfo);
    }

    @Override
    public QmsWindowInfo checkWindowExistByUuid(String windowUuid) throws Exception {
        QmsWindowInfo qmsWindowInfo = qmsWindowInfoMapper.selectQmsWindowInfoByUUID(windowUuid);
        Assert.notNull(qmsWindowInfo, ErrorTipsConst.TIPS_WINDOW_NOT_FOUND);
        return qmsWindowInfo;
    }


    /**
     * 批量绑定鹤位屏
     *
     * @param windowId 窗口ID
     * @param berrIds 需要绑定的鹤位屏ID
     * @return 结果
     */
    @Override
    public int insertBindBerrs(Long windowId, Long[] berrIds)
    {
        // 新增用户与角色管理
        List<QmsWindowBerr> list = new ArrayList<QmsWindowBerr>();
        for (Long berrId : berrIds)
        {
            QmsWindowBerr qmsWindowBerr = new QmsWindowBerr();
            qmsWindowBerr.setWindowId(windowId);
            qmsWindowBerr.setBerrId(berrId);
            list.add(qmsWindowBerr);
        }
        return qmsWindowBerrMapper.batchWindowBerr(list);
    }

    /**
     * 批量取消绑定鹤位屏
     *
     * @param windowId 窗口ID
     * @param berrIds 鹤位屏IDs
     * @return 结果
     */
    @Override
    public int deleteWindowBerrs(Long windowId, Long[] berrIds)
    {
        return qmsWindowBerrMapper.deleteWindowBerrInfos(windowId, berrIds);
    }

}
